package com.bocai.yuweiwangluo.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import com.bocai.yuweiwangluo.R;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by admin on 2016/1/23.
 */
public class ImagePathInitUtil {
    public static String IMAGE_PATH="";
    private static final String FILE_NAME = "/share_image.jpg";
    public static String  initImagePath(Context context) {
        try {
            if (Environment.MEDIA_MOUNTED.equals(Environment
                    .getExternalStorageState())
                    && Environment.getExternalStorageDirectory().exists()) {
                IMAGE_PATH = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + FILE_NAME;
            } else {
                IMAGE_PATH =context.getFilesDir().getAbsolutePath()
                        + FILE_NAME;
            }
            // 创建图片文件夹
            File file = new File(IMAGE_PATH);
            if (!file.exists()) {
                file.createNewFile();
                Bitmap pic = BitmapFactory.decodeResource(context.getResources(),
                        R.mipmap.shareimage);
                FileOutputStream fos = new FileOutputStream(file);
                pic.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
            }
        } catch (Throwable t) {
            t.printStackTrace();
            IMAGE_PATH = null;
        }
        return IMAGE_PATH;
    }
}
